package com.bridge.common.http;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;


/**
 * Created by Administrator on 2017/1/9.
 */

public class ToastUtil {
    private static String oldMsg;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

    public static void showToast(Context context, int resId) {
        showToast(context, context.getString(resId));
    }

    public static void showToast(Context context, int resId, int gravity) {
        showToast(context, context.getString(resId), gravity, 0, 0);
    }

    public static void showToast(Context context, String s, int gravity) {
        showToast(context, s, gravity, 0, 0);
    }

    public static void showToast(Context context, int resId, int gravity, int offX, int offY) {
        showToast(context, context.getString(resId), gravity, offX, offY);
    }

    public static void showToast(Context context, String s) {
        if (toast == null) {
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }


    public static void showToast(Context context, String s, int gravity, int offX, int offY) {
        if (toast == null) {
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.setGravity(gravity, offX, offY);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }
//    private static TextView textView;
//    /**
//     * 自定义样式的吐司
//     * <p/>
//     * 静态toast 只创建一个toast实例 可以实时显示弹出的内容
//     *
//     * @param context
//     * @param text
//     */
//    public static void showPicToast(Context context, String text) {
//
//        if (toast == null) { // 1. 创建前 2.消失后toast为null
//            // 获取打气筒
//            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            //创建视图
//            View view = inflater.inflate(R.layout.toast_bg, null);
//            textView = (TextView) view.findViewById(R.id.tv_toast_text);
//            //创建土司
//            toast = new Toast(context);
//            //设置居中方式  默认在底部
//            //toast.setGravity(Gravity.CENTER, 0, 0);//如果不设置剧中方式,使用系统默认的吐司位置
//            //设置土司的持续时长
//            toast.setDuration(Toast.LENGTH_SHORT);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            //设置土司的背景View
//            toast.setView(view);
//        }
//        //设置土司的显示额内容
//        textView.setText(text);
//        toast.show();
//    }
}